<?php

declare(strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class Refund extends Base
{
    protected $name = 'refund';
    
    protected $type = [
        'confirm_time' => 'timestamp',
    ];
    public function order()
    {
        return $this->hasOne(Order::class, 'id', 'order_id')->field('id,sn');
    }
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,photo,mobile,level_id,parent_id');
    }
    public function express()
    {
        return $this->hasOne(Express::class, 'id', 'express_id')->field('id,title');
    }

    public static function _save($param, $logs)
    {

        parent::startTrans();
        try {
            if (!isset($param['status'])) return ['code' => -1, 'msg' => '请选择审核类型'];

            $info = parent::find($param['id']);
            $order = Order::where('id', $info->order_id)->find();
            if (empty($info)) return ['code' => -1, 'msg' => '审核失败'];
            if ($order->getData('status') == 6) {
                return ['code' => -1, 'msg' => '此笔记录已退款，请勿重复审核！'];
            }
            if ($param['status'] == 2) {
                if ($info->type == 1) {
                    $order->status = 6;
                }else{
                    $order->status = 5;
                }
                $msg = '同意退款，ID【' . $param['id'] . '】';
            } else if ($param['status'] == 4) {
                $order->status = $info->refund_status;
                $msg = '拒绝退款，ID【' . $param['id'] . '】';
            } else {
                $msg = '退款成功,ID【' . $param['id'] . '】';
                $order->status = 6;
                if (in_array($order->getData('pay_way'), ['currency', 'bonus'])) {
                    Target::Account()->basics($info['uid'], $order->getData('pay_way'), $order['real_amount'], '商品退款', '+')
                        ->createNormal();
                }
            }
            $order->save();
            $info->status = $param['status'];
            if ($param['status'] == 4) {
                $info->reject_msg = $param['reject_msg'];
            }
            $info->confirm_time = time();
            $info->admin_id = $logs['admin']->id;
            $info->admin_name = $logs['admin']->admin_name;
            $info->save();
            Logs::adds($msg, $logs['admin']->id, $logs['admin']->admin_name);
            parent::commit();
            return ['code' => 1, 'msg' => '已审核'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 申请退款
     */
    public static function refund_order($param, $user)
    {
        parent::startTrans();
        try {
            $info = Order::where(['id' => $param['id'], 'uid' => $user->id])->find();
            if (empty($info)) {
                return ['code' => -1, 'msg' => '订单不存在'];
            }
            if ($info->getData('status') == 6) {
                return ['code' => -1, 'msg' => '该订单已退款不可重复申请！'];
            }
            $find = parent::where(['uid' => $user->id, 'order_id' => $info['id']])->find();
            $data['reason'] = $param['reason'];
            $data['note'] = $param['note'];
            $data['images'] = $param['images'];
            $data['type'] = $param['type'];
            $data['refund_status'] = $info->getData('status');
            if (empty($find)) {
                $data['order_id'] = $info['id'];
                $data['uid'] = $user['id'];
                $msg ='申请退款成功！请耐心等待商家审核！';
                parent::create($data);
            }else{
                if($find->status == 4){
                    $data['status'] = 1;
                    $msg ='申请退款成功！请耐心等待商家审核！';
                    parent::where('id',$find->id)->update($data);
                }else if($find->status == 5){
                    $msg ='订单已退款！不可重复提交！';
                }
            }
            $info->status = 5;
            $info->save();
            parent::commit();
            return ['code' => 1, 'msg' => $msg, 'url' => '/index/order/my_order.html'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
